//
//  stock20.cpp
//  Practice12
//
//  Created by 赵世杰 on 2020/8/15.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#include "stock20.hpp"
#include <iostream>

Stock :: Stock()
{
    company = "no name";
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
    
}


Stock :: Stock(const string & co , long n , double pr)
{
    
    company = new char[co.length()];
    
    strcpy(company, co.c_str());
    
    if(n < 0)
    {
        cout << "Number of shares can't be negative";
        
        shares = 0;
    }
    else
    {
        shares = n;
    }
    
    share_val = pr;
    
    set_tot();
    
}

Stock :: ~Stock()
{
    delete [] company;
}

void Stock::buy(long num, double price)
{
    if(num < 0)
    {
        cout << "Number of shares purchased cna't be negative. " << "Transaction is aborted. \n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void Stock::sell(long num, double price)
{
    if(num < 0)
    {
        cout << "Number of shares sold cna't be negative. " << "Transaction is aborted. \n";
    }
    else if (num > shares)
    {
        cout << "You can't sell more than you have!";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}


void Stock::updata(double price)
{
    share_val = price;
    set_tot();
}


void Stock::show() const
{
    using std::cout ;
    using std::ios_base;
    
    ios_base::fmtflags orig = cout.setf(ios_base::fixed , ios_base::floatfield);
    
    std::streamsize prec = cout.precision(3);
    
    cout << "Company: " << company << " Shares: " << shares << '\n';
    cout << "Share Price: $ " << share_val;
    cout.precision(2);
    
    cout << " Total Worth: $" << total_val << "\n";
    
    cout.setf(orig,ios_base::floatfield);
    
    cout.precision(prec);
    
}

const Stock & Stock::topval(const Stock &s) const
{
    if(s.total_val > total_val)
    {
        return s;
    }
    else
    {
        return *this;
    }
}





